Programming Questions for Practice
Write a program to perform multiplication of two matrices.
#include <stdio.h>
#include <stdlib.h>
/* Write a program to perform multiplication of two matrices. */
int main()
{
// Creating matrices of given size
int rows, columns;
printf("Enter the number of rows and columns: ");
scanf("%d%d", &rows, &columns);
int matrix_A[rows][columns], matrix_B[rows][columns], matrix_result[rows][columns];
// Reading the matrices
printf("Enter %d values for matrix A: ", rows*columns);
for(int row = 0; row < rows; row++)
for(int column = 0; column < columns; column++)
scanf("%d", &matrix_A[row][column]);
printf("Enter %d values for matrix B: ", rows*columns);
for(int row = 0; row < rows; row++)
for(int column = 0; column < columns; column++)
scanf("%d", &matrix_B[row][column]);
//display matrices
printf("\nMatrix A is\n");
for(int row = 0; row < rows; row++) {
for(int column = 0; column < columns; column++)
printf("%d\t", matrix_A[row][column]);
printf("\n");
}
printf("\nMatrix B is\n");
for(int row = 0; row < rows; row++) {
for(int column = 0; column < columns; column++)
printf("%d\t", matrix_B[row][column]);
printf("\n");
}
// Logic of multiplication two matrices
for(int row = 0; row < rows; row++) {
for(int column = 0; column < columns; column++) {
matrix_result[row][column] = 0;
for(int temp = 0; temp < columns; temp++)
matrix_result[row][column] += matrix_A[row][temp] * matrix_B[temp][column];
}
}
// Display the result
printf("\nResult of A * B is\n");
for(int row = 0; row < rows; row++) {
for(int column = 0; column < columns; column++)
printf("%d\t", matrix_result[row][column]);
printf("\n");
}
return 0;
}
Code not available...
Code not available...